# -*- coding: utf-8 -*-

import time
import re
import csv 
import os

os.chdir(os.path.expanduser("~/Dropbox/Research/termlimited/data/FL/house_journals"))


write_or_append = 'w'
outputfilename = 'fl_legislators_bills.csv'
file = open(outputfilename, write_or_append) # file is used for appending
output = csv.writer( file, delimiter=';', dialect='excel')
if write_or_append=='w':
	output.writerow(['year', 'dist' , 'name', 'sponsored', 'cosponsored', 'local', 'com'])




for y in [1990,1991,1992, 1993, 1994, 1995,1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016]: #
	year = str(y)
	text_file = open("sponsors_regular_"+str(y)+".txt", "r")
	text = text_file.read().lower()
	leg = text.split("xxxxxxxxxx") 
	for legislator in leg:
		try:
			tmp=dist=firstname=lastname=sponsored=cosponsored=local=com=None 
			tmp = re.search('(.*[\n]?.*)district', legislator ).group(1).replace("\n","").strip() # [—-]
			dist = re.search('([0-9]+)', tmp ).group(1).replace("\n","").strip()
			name = re.search('([a-z]+, [ .a-z]+)', tmp ).group(1).replace("\n","").strip()
			if re.search('sponsored: ([0-9, \n]+)', legislator):
				sponsored = re.search('sponsored: ([0-9, \n]+)', legislator).group(1).replace("\n","").strip()
			if re.search('co-?sponsored: ([0-9, \n]+)', legislator):
				cosponsored = re.search('co-?sponsored: ([0-9, \n]+)', legislator).group(1).replace("\n","").strip()
			if re.search('local bills: ([0-9, \n]+)', legislator):
				local = re.search('local bills: ([0-9, \n]+)', legislator).group(1).replace("\n","").strip()
			if re.search('committees: (.*)', legislator):
				com = re.search('committees: (.*)', legislator).group(1).replace("\n","").strip()
			output.writerow([ year, dist, name, sponsored, cosponsored, local, com])	
			print name, year
		except:
			"error"
file.close()








x

def find_between( s, first, last ):
    try:
        start = s.index( first ) + len( first )
        end = s.index( last, start )
        return s[start:end]
    except ValueError:
        return ""
